/**
 * 支付页面只加载购物车中checked=true的数据
 * 
 * 点击支付按钮判断缓存中是否有token
 * 有的支付
 * 没有跳转授权页面 获取token
 */
import {
  getSetting,
  openSetting,
  chooseAddress,
  showModel,
  showToast
} from "../../utils/WxApi";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({
  data: {
    address: {},
    cartItem: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow: function() {
    //获取收货地址 添加到缓存中
    const loaclAddress = wx.getStorageSync("address");
    //获取缓存中的商品添加到购物车页面中
    let cartItem = wx.getStorageSync("cart") || [];
    cartItem = cartItem.filter(v => v.checked);
    let totalNum = 0;
    //总数量
    let totalPrice = 0;
    //计算总价格总数量
    cartItem.forEach(v => {
      (totalNum += v.num), (totalPrice += v.num * v.goods_price);
    });
    this.setData({
      cartItem,
      totalNum,
      totalPrice,
      address: loaclAddress
    });
    wx.setStorageSync("cart", cartItem);
  },
  //去支付
  hanldePay(){
    const token = wx.getStorageSync('token');
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index',
      });
    }
  }
});
